﻿#region Usings

using System.Linq;
using System.Windows;
using System.Windows.Markup;
using SilverBox.Logging;
using SilverBox.Logging.Providers;

#endregion

namespace SilverBox.Services
{
    ///<summary>
    ///</summary>
    [ContentProperty("Children")]
    public class LoggerService : DependencyObject, IApplicationService, ILogger<object>
    {
        private static LoggerService _current;


        ///<summary>
        ///</summary>
        public static LoggerService Current
        {
            get { return _current; }
        }

        public LoggerCollection Children { get; set; }

        public bool IsRunning { get; internal set; }

        #region Implementation of IApplicationService

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void StartService(ApplicationServiceContext context)
        {
            _current = this;
            IsRunning = true;
        }

        /// <summary>
        /// 
        /// </summary>
        public void StopService()
        {
            _current = null;
            IsRunning = false;
        }

        #endregion

        #region ILogger<object> Members

        public void Log(object entry)
        {
            foreach (var logger in Children.Where(l => l.Check(entry)))
            {
                Log(entry, logger);
            }
        }

        #endregion

        public void Log(string entry, ILoggerProvider provider)
        {
            provider.Log(entry);
        }

        public void Log(object entry, Logger logger)
        {
            logger.Log(entry);
        }
    }
}